﻿using System;
using System.Configuration;
using System.Linq;
using System.Web;
using AlipaySDK.Helper;
using Common.Logging;
using Newtonsoft.Json;
using OutDbContext;
using User.Api.BizExtensions;

namespace User.Api.Helpers
{
    public class MessageHelper
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(MessageHelper));
        private static readonly string WxOauth2State = "GbkYin666";
        private static readonly string AppId  = ConfigurationManager.AppSettings["WeixinAppId"];
        private static readonly string HeadColor = "#0BA6E4";
        private static readonly string keywordColor = "#000000";

        //住院缴费成功
        //{{first.value}}
        //就诊人：{{keyword1.value}}
        //住院号：{{keyword2.value}}
        //金额：{{keyword3.value}}
        //时间：{{keyword4.value}}
        //订单号：{{keyword5.value}}
        //{{remark.value}}
        private static readonly string ZhuYuan = "ad15777507b248d0a0a35d598e847a07";

        //门诊缴费成功
        //{{first.value}}
        //流水号：{{keyword1.value}}
        //医院名称：{{keyword2.value}}
        //就诊人：{{keyword3.value}}
        //就诊卡号：{{keyword4.value}}
        //缴费金额：{{keyword5.value}}
        //{{remark.value}}
        private static readonly string MenZhen = "df931453c4dc4d6c86558b9df21ad5bd";	

        //预约挂号成功通知	
        //{{first.value}}
        //就诊人：{{keyword1.value}}
        //就诊卡号：{{keyword2.value}}
        //挂号科室：{{keyword3.value}}
        //看诊医生：{{keyword4.value}}
        //就诊时间：{{keyword5.value}}
        //{{remark.value}}
        private static readonly string GuaHao = "5a9746f668e346c883f7cefd2fffe8ee";

        //医生停诊
        //{{first.value}}
        //科室：{{keyword1.value}}
        //医生：{{keyword2.value}}
        //就诊人：{{keyword3.value}}
        //就诊时间：{{keyword4.value}}
        //挂号金额：{{keyword5.value}}
        //{{remark.value}}
        private static readonly string TingZhen = "dd4c8e63bbc5430a832e0a39183b4fce";
        #region

        #endregion
        /// <summary>
        /// 获取网页地址
        /// </summary>
        /// <param name="routePath"></param>
        /// <returns></returns>
        public static string GetH5Url(string routePath)
        {
            var h5BaseAddress = ConfigurationManager.AppSettings["H5BaseAddress"];
            var baseAddress = ConfigurationManager.AppSettings["BaseAddress"];
            //var appId = ConfigurationManager.AppSettings["WeixinAppId"];

            var url = h5BaseAddress + routePath;
            var redirectUrl = baseAddress + "api/Auth/WxOauth2?url=" + HttpUtility.UrlEncode(url);
            //https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2015052700092241&auth_skip=false&scope=auth_userinfo&state={WxOauth2State}&redirect_uri=http://wx.e3861.com/api/Auth/WxOauth2?url=http://wx.e3861.com/h5/index.html#!/index/get-queue
            var oauth2Url =
                $"https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id={AppId}&auth_skip=false&scope=auth_userinfo&state={WxOauth2State}&redirect_uri={HttpUtility.UrlEncode(redirectUrl)}";

            return oauth2Url;
        }


        /// <summary>
        /// 获取支付宝支付成功跳转地址
        /// </summary>
        /// <param name="routePath"></param>
        /// <returns></returns>
        public static string GetCallBackUrl(string routePath)
        {
            var h5BaseAddress = ConfigurationManager.AppSettings["H5BaseAddress"];
            var url = h5BaseAddress + routePath;

            return url;
        }

        /// <summary>
        /// 发送挂号成功消息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        /// <returns></returns>
        public static void SendRegSucceedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.ApptOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }
                var user = ctx.WxUsers.FirstOrDefault(x => x.Id == order.WxUserId);
                if (user == null)
                {
                    return;
                }

                var url = GetH5Url("/index/reg-order-detail/" + order.Id);

                //发送模板信息
                var bizContent = new
                {
                    toUserId = user.OpenId,
                    template = new
                    {
                        templateId = GuaHao,
                        context = new
                        {
                            actionName="查看详情",
                            headColor= HeadColor,
                            url= url,
                            keyword1 = new
                            {
                                color= keywordColor,
                                value = order.CardName
                            },
                            keyword2 = new
                            {
                                color = keywordColor,
                                value = order.CardNo
                            },
                            keyword3 = new
                            {
                                color = keywordColor,
                                value = order.DeptName
                            },
                            keyword4 = new
                            {
                                color = keywordColor,
                                value = order.DoctorName
                            },
                            keyword5 = new
                            {
                                color = keywordColor,
                                value = order.RegDate
                            },
                            first = new
                            {
                                color = HeadColor,
                                value = "挂号成功"
                            },
                            remark = new
                            {
                                color = HeadColor,
                                value = ""
                            }
                        }

                    }
                };

                //var message =
                //    string.Format(
                //    "挂号成功！\r" +
                //    "订单号：" + order.OrderNo + "\r" +
                //    "姓名：{0}\r" +
                //    "卡号：{1}\r" +
                //    "科室：{2}\r" +
                //    "医生：{3}\r" +
                //    "就诊日期：{4}\r" +
                //    "挂号费用：{5} 元\r\n" +
                //    "<a href='{6}'>点这里【查看挂号单】</a>",
                //    order.CardName,
                //    order.CardNo,
                //    order.DeptName,
                //    order.DoctorName,
                //    order.RegDate,
                //    order.SumFee.GetValueOrDefault().ToString("F2"),
                //    url);

                //if (order.DeptName == "儿科门诊(番禺)" || order.DeptName == "儿童保健科门诊(番禺)" || order.DeptName == "产科门诊(番禺)" || order.DeptName == "产前诊断门诊(番禺)")
                //{
                //    message += "\r\n温馨提示：\r\n请提前15分钟到护士站报到确认！因故迟到、未超过预约就诊时间30分钟者，报到确认后，按系统排在当前三位候诊患者之后就诊；迟到超过30分钟以上、未超过正常下班时间者，系统安排在本班次最后就诊或者退号后重新挂号排队就诊。请见门诊就诊秩序。";
                //}

                try
                {
                    //SendMessage.SendText(AppId, user.OpenId, message);
                    SendMessage.SendTemplate(AppId, JsonConvert.SerializeObject(bizContent));
                }

                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送挂号成功消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送挂号失败消息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        public static void SendRegFailedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.ApptOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }
                var user = ctx.WxUsers.FirstOrDefault(x => x.Id == order.WxUserId);
                if (user == null)
                {
                    return;
                }
                var message =
                    string.Format(
                    "挂号失败！\r" +
                    "订单号：" + order.OrderNo + "\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r" +
                    "科室：{2}\r" +
                    "医生：{3}\r" +
                    "就诊日期：{4}\r" +
                    "挂号费用：{5} 元",
                    order.CardName,
                    order.CardNo,
                    order.DeptName,
                    order.DoctorName,
                    order.RegDate,
                    order.SumFee.GetValueOrDefault().ToString("F2"));

                if (order.SumFee > 0)
                {
                    message += "\r\n系统正在执行退费操作，稍后您的所缴纳的款项将会退回您的账户中";
                }

                try
                {
                   // CustomApi.SendText(null, order.OpenId, message);
                    SendMessage.SendText(AppId, user.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送挂号失败消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送挂号失败退费成功消息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        public static void SendRegFailedRefundSucceedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.ApptOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }
                var user = ctx.WxUsers.FirstOrDefault(x => x.Id == order.WxUserId);
                if (user == null)
                {
                    return;
                }
                var message =
                    string.Format(
                    "退费成功！\r" +
                    "挂号单信息：\r" +
                    "订单号：" + order.OrderNo + "\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r" +
                    "科室：{2}\r" +
                    "医生：{3}\r" +
                    "就诊日期：{4}\r" +
                    "挂号费用：{5} 元\r\n" +
                    "您所缴纳的款项已退回。支付宝支付即刻到账，银行卡支付7个工作日内到账。请注意查收",
                    order.CardName,
                    order.CardNo,
                    order.DeptName,
                    order.DoctorName,
                    order.RegDate,
                    order.SumFee.GetValueOrDefault().ToString("F2"));

                try
                {
                    //CustomApi.SendText(null, order.OpenId, message);
                    SendMessage.SendText(AppId, user.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送挂号失败退费成功消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送退号成功消息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        public static void SendUnpickupSucceedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.ApptOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }
                var user = ctx.WxUsers.FirstOrDefault(x => x.Id == order.WxUserId);
                if (user == null)
                {
                    return;
                }
                var message =
                    string.Format(
                    "退号成功！\r" +
                    "订单号：" + order.OrderNo + "\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r" +
                    "科室：{2}\r" +
                    "医生：{3}\r" +
                    "就诊日期：{4}\r" +
                    "挂号费用：{5} 元",
                    order.CardName,
                    order.CardNo,
                    order.DeptName,
                    order.DoctorName,
                    order.RegDate,
                    order.SumFee.GetValueOrDefault().ToString("F2"));

                if (order.SumFee > 0)
                {
                    message += "\r\n系统正在执行退费操作，稍后您的所缴纳的款项将会退回您的账户中";
                }

                try
                {
                    //CustomApi.SendText(null, order.OpenId, message);
                    SendMessage.SendText(AppId, user.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送退号成功消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送退号失败消息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        /// <returns></returns>
        public static void SendUnpickupFailedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.ApptOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }
                var user = ctx.WxUsers.FirstOrDefault(x => x.Id == order.WxUserId);
                if (user == null)
                {
                    return;
                }
                var url = GetH5Url("/index/reg-order-detail/" + order.Id);


                //发送模板信息
                var bizContent = new
                {
                    toUserId = user.OpenId,
                    template = new
                    {
                        templateId = GuaHao,
                        context = new
                        {
                            actionName = "查看详情",
                            headColor = HeadColor,
                            url = url,
                            keyword1 = new
                            {
                                color = keywordColor,
                                value = order.CardName
                            },
                            keyword2 = new
                            {
                                color = keywordColor,
                                value = order.CardNo
                            },
                            keyword3 = new
                            {
                                color = keywordColor,
                                value = order.DeptName
                            },
                            keyword4 = new
                            {
                                color = keywordColor,
                                value = order.DoctorName
                            },
                            keyword5 = new
                            {
                                color = keywordColor,
                                value = order.RegDate
                            },
                            first = new
                            {
                                color = HeadColor,
                                value = "退号失败"
                            },
                            remark = new
                            {
                                color = HeadColor,
                                value = ""
                            }
                        }

                    }
                };

                //var message =
                //    string.Format(
                //    "退号失败！\r" +
                //    "订单号：" + order.OrderNo + "\r" +
                //    "姓名：{0}\r" +
                //    "卡号：{1}\r" +
                //    "科室：{2}\r" +
                //    "医生：{3}\r" +
                //    "就诊日期：{4}\r" +
                //    "挂号费用：{5} 元\r\n" +
                //    "<a href='{6}'>点这里【查看挂号单】</a>",
                //    order.CardName,
                //    order.CardNo,
                //    order.DeptName,
                //    order.DoctorName,
                //    order.RegDate,
                //    order.SumFee.GetValueOrDefault().ToString("F2"),
                //    url);

                try
                {
                    //SendMessage.SendText(AppId, user.OpenId, message);
                    SendMessage.SendTemplate(AppId, JsonConvert.SerializeObject(bizContent));
                }

                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送退号失败消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送退号成功退费成功消息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        public static void SendUnpickupSucceedRefundSucceedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.ApptOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }
                var user = ctx.WxUsers.FirstOrDefault(x => x.Id == order.WxUserId);
                if (user == null)
                {
                    return;
                }
                var message =
                    string.Format(
                    "退费成功！\r" +
                    "挂号单信息：\r" +
                    "订单号：" + order.OrderNo + "\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r" +
                    "科室：{2}\r" +
                    "医生：{3}\r" +
                    "就诊日期：{4}\r" +
                    "挂号费用：{5} 元\r\n" +
                    "您所缴纳的款项已退回。支付宝支付即刻到账，银行卡支付7个工作日内到账。请注意查收",
                    order.CardName,
                    order.CardNo,
                    order.DeptName,
                    order.DoctorName,
                    order.RegDate,
                    order.SumFee.GetValueOrDefault().ToString("F2"));

                try
                {
                    //CustomApi.SendText(null, order.OpenId, message);
                    SendMessage.SendText(AppId, user.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送退号成功退费成功消息出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送缴费成功通知
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        public static void SendBillSucceedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.BillOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }
                var user = ctx.WxUsers.FirstOrDefault(x => x.Id == order.WxUserId);
                if (user == null)
                {
                    return;
                }
                var url = GetH5Url("/index/bill-order-detail/" + order.Id);


                //发送模板信息
                var bizContent = new
                {
                    toUserId = user.OpenId,
                    template = new
                    {
                        templateId = MenZhen,
                        context = new
                        {
                            actionName = "查看详情",
                            headColor = HeadColor,
                            url = url,
                            keyword1 = new
                            {
                                color = keywordColor,
                                value = order.ClinicNo
                            },
                            keyword2 = new
                            {
                                color = keywordColor,
                                value = "河源市中医院"
                            },
                            keyword3 = new
                            {
                                color = keywordColor,
                                value = order.CardName
                            },
                            keyword4 = new
                            {
                                color = keywordColor,
                                value = order.CardNo
                            },
                            keyword5 = new
                            {
                                color = keywordColor,
                                value = order.PrescMoney.GetValueOrDefault().ToString("F2")
                            },
                            first = new
                            {
                                color = HeadColor,
                                value = "缴费成功"
                            },
                            remark = new
                            {
                                color = HeadColor,
                                value = ""
                            }
                        }

                    }
                };


                //var yibaoMsg = "";
                //if (order.YiBaoStatus == BillOrderExtensions.YiBaoStatus.使用了医保.ToInt32()
                //    && !string.IsNullOrEmpty(order.YiBaoJydjh))
                //{
                //    yibaoMsg = string.Format(
                //        "个人支付：{0} 元\r" +
                //        "医保支付：{1} 元\r",
                //        order.PersonalMoney.GetValueOrDefault().ToString("F2"),
                //        order.YibaoMoney.GetValueOrDefault().ToString("F2")
                //        );
                //}

                //var message =
                //    string.Format(
                //    "缴费成功！\r" +
                //    "订单号：" + order.OrderNo + "\r" +
                //    "姓名：{0}\r" +
                //    "卡号：{1}\r" +
                //    "就诊流水号：{2}\r" +
                //    "开单时间：{3}\r" +
                //    "开单科室：{4}\r" +
                //    yibaoMsg +
                //    "总费用：{5} 元\r\n" +
                //    "<a href='{6}'>点这里【查看缴费单】</a>"
                //    + "\r\n 如需补打发票，请于当天移步人工窗口办理，过期无效。",
                //    order.CardName,
                //    order.CardNo,
                //    order.ClinicNo,
                //    order.OrderDate,
                //    order.OrderDept,
                //    order.PrescMoney.GetValueOrDefault().ToString("F2"),
                //    url);

                try
                {
                    //SendMessage.SendText(AppId, user.OpenId, message);
                    SendMessage.SendTemplate(AppId, JsonConvert.SerializeObject(bizContent));
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送缴费成功消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送建卡失败
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        public static void SendBillFailedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.BillOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }
                var user = ctx.WxUsers.FirstOrDefault(x => x.Id == order.WxUserId);
                if (user == null)
                {
                    return;
                }
                var message =
                    string.Format(
                    "缴费失败！\r" +
                    "订单号：" + order.OrderNo + "\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r" +
                    "就诊流水号：{2}\r" +
                    "开单时间：{3}\r" +
                    "开单科室：{4}\r" +
                    "总费用：{5} 元\r",
                    order.CardName,
                    order.CardNo,
                    order.ClinicNo,
                    order.OrderDate,
                    order.OrderDept,
                    order.PrescMoney.GetValueOrDefault().ToString("F2"));

                message += "\r\n系统正在执行退费操作，稍后您的所缴纳的款项将会退回您的账户中";

                try
                {
                    // CustomApi.SendText(null, order.OpenId, message);
                    SendMessage.SendText(AppId, user.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送缴费失败消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }
        /// <summary>
        /// 发送建卡失败通知
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        public static void JianKaMsg(string OpenId, string message)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var user = ctx.WxUsers.FirstOrDefault(x => x.OpenId == OpenId);
                if (user == null)
                {
                    return;
                }
                try
                {
                   // CustomApi.SendText(null, order.OpenId, message);
                    SendMessage.SendText(AppId, user.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送缴费失败消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送缴费失败退费成功消息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        public static void SendBillFailedRefundSucceedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.BillOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }
                var user = ctx.WxUsers.FirstOrDefault(x => x.Id == order.WxUserId);
                if (user == null)
                {
                    return;
                }
                var message =
                    string.Format(
                    "退费成功！\r" +
                    "缴费单信息：\r" +
                    "订单号：" + order.OrderNo + "\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r" +
                    "就诊流水号：{2}\r" +
                    "开单时间：{3}\r" +
                    "开单科室：{4}\r" +
                    "总费用：{5} 元\r\n" +
                    "您所缴纳的款项已退回。支付宝支付即刻到账，银行卡支付7个工作日内到账。请注意查收",
                    order.CardName,
                    order.CardNo,
                    order.ClinicNo,
                    order.OrderDate,
                    order.OrderDept,
                    order.PrescMoney.GetValueOrDefault().ToString("F2"));

                try
                {
                    //CustomApi.SendText(null, order.OpenId, message);
                    SendMessage.SendText(AppId, user.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送缴费失败退费成功消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送住院预缴金成功通知
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        public static void SendPrepaySucceedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.InpatientPrepayOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }

                var url = GetH5Url("/index/prepay-order-detail/" + order.Id);

                //发送模板信息
                var bizContent = new
                {
                    toUserId = order.OpenId,
                    template = new
                    {
                        templateId = ZhuYuan,
                        context = new
                        {
                            actionName = "查看详情",
                            headColor = HeadColor,
                            url = url,
                            keyword1 = new
                            {
                                color = keywordColor,
                                value = order.CardName
                            },
                            keyword2 = new
                            {
                                color = keywordColor,
                                value = order.InpatientId
                            },
                            keyword3 = new
                            {
                                color = keywordColor,
                                value = order.PrepayMoney.GetValueOrDefault().ToString("F2")
                            },
                            keyword4 = new
                            {
                                color = keywordColor,
                                value = order.PayTime.ToStringOrDefault()
                            },
                            keyword5 = new
                            {
                                color = keywordColor,
                                value = order.OrderNo
                            },
                            first = new
                            {
                                color = HeadColor,
                                value = "住院预交金缴费成功"
                            },
                            remark = new
                            {
                                color = HeadColor,
                                value = ""
                            }
                        }

                    }
                };
                //var message =
                //    string.Format(
                //    "住院预缴金缴纳成功\r" +
                //    "订单号：" + order.OrderNo + "\r" +
                //    "姓名：{0}\r" +
                //    "卡号：{1}\r" +
                //    "充值金额：{2} 元\r\n" +
                //    "<a href='{3}'>点这里【查看住院预缴单】</a>",
                //    order.CardName,
                //    order.CardNo,
                //    order.PrepayMoney.GetValueOrDefault().ToString("F2"),
                //    url);

                try
                {
                    //SendMessage.SendText(AppId, order.OpenId, message);
                    SendMessage.SendTemplate(AppId, JsonConvert.SerializeObject(bizContent));
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送住院预缴金成功消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送住院预缴金失败通知
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        public static void SendPrepayFailedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.InpatientPrepayOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }

                var message =
                    string.Format(
                    "住院预缴金缴纳失败！\r" +
                    "订单号：" + order.OrderNo + "\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r" +
                    "充值金额：{2} 元\r",
                    order.CardName,
                    order.CardNo,
                    order.PrepayMoney.GetValueOrDefault().ToString("F2"));

                message += "\r\n系统正在执行退费操作，稍后您的所缴纳的款项将会退回您的账户中";

                try
                {
                    //CustomApi.SendText(null, order.OpenId, message);
                    SendMessage.SendText(AppId, order.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送住院预缴金失败消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送住院预缴金失败退费成功消息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        public static void SendPrepayFailedRefundSucceedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.InpatientPrepayOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }

                var message =
                    string.Format(
                    "退费成功！\r" +
                    "住院预缴单信息：\r" +
                    "订单号：" + order.OrderNo + "\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r" +
                    "充值金额：{2} 元\r\n" +
                    "您所缴纳的款项已退回。支付宝支付即刻到账，银行卡支付7个工作日内到账。请注意查收",
                    order.CardName,
                    order.CardNo,
                    order.PrepayMoney.GetValueOrDefault().ToString("F2"));

                try
                {
                    //CustomApi.SendText(null, order.OpenId, message);
                    SendMessage.SendText(AppId, order.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送住院预缴金失败退费成功消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送点餐成功消息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        /// <returns></returns>
        public static void SendDinnerSucceedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.DinnerOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }

                var url = GetH5Url("/index/dinner-order-detail/" + order.Id);

                var message =
                    string.Format(
                    "点餐成功！\r" +
                    "订单号：" + order.OrderNo + "\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r" +
                    "病区：{2}\r" +
                    "床号：{3}\r" +
                    "日期：{4}\r" +
                    "餐点：{5}\r" +
                    "菜品数量：{6}\r" +
                    "总金额：{7} 元\r\n" +
                    "<a href='{8}'>点这里【查看点餐单】</a>",
                    order.CardName,
                    order.CardNo,
                    order.WardName,
                    order.BedLabel,
                    order.DinnerDate,
                    "[" + order.FpName + "]" + order.HmtName,
                    order.FoodCount,
                    order.TotalMoney.GetValueOrDefault().ToString("F2"),
                    url);

                try
                {
                    //CustomApi.SendText(null, order.OpenId, message);
                    SendMessage.SendText(AppId, order.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送点餐成功消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送点餐失败消息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        public static void SendDinnerFailedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.DinnerOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }

                var message =
                    string.Format(
                    "点餐失败！\r" +
                    "订单号：" + order.OrderNo + "\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r" +
                    "病区：{2}\r" +
                    "床号：{3}\r" +
                    "日期：{4}\r" +
                    "餐点：{5}\r" +
                    "菜品数量：{6}\r" +
                    "总金额：{7} 元",
                    order.CardName,
                    order.CardNo,
                    order.WardName,
                    order.BedLabel,
                    order.DinnerDate,
                    "[" + order.FpName + "]" + order.HmtName,
                    order.FoodCount,
                    order.TotalMoney.GetValueOrDefault().ToString("F2"));

                message += "\r\n系统正在执行退费操作，稍后您的所缴纳的款项将会退回您的账户中";

                try
                {
                    //CustomApi.SendText(null, order.OpenId, message);
                    SendMessage.SendText(AppId, order.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送点餐失败消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送点餐失败退费成功消息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        public static void SendDinnerFailedRefundSucceedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.DinnerOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }

                var message =
                    string.Format(
                    "退费成功！\r" +
                    "点餐单信息：\r" +
                    "订单号：" + order.OrderNo + "\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r" +
                    "病区：{2}\r" +
                    "床号：{3}\r" +
                    "日期：{4}\r" +
                    "餐点：{5}\r" +
                    "菜品数量：{6}\r" +
                    "总金额：{7} 元\r\n" +
                    "您所缴纳的款项已退回。支付宝支付即刻到账，银行卡支付7个工作日内到账。请注意查收",
                    order.CardName,
                    order.CardNo,
                    order.WardName,
                    order.BedLabel,
                    order.DinnerDate,
                    "[" + order.FpName + "]" + order.HmtName,
                    order.FoodCount,
                    order.TotalMoney.GetValueOrDefault().ToString("F2"));

                try
                {
                    //CustomApi.SendText(null, order.OpenId, message);
                    SendMessage.SendText(AppId, order.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送点餐失败退费成功消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送退餐成功消息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        public static void SendUnDinnerSucceedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.DinnerOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }

                var message =
                    string.Format(
                    "退餐成功！\r" +
                    "订单号：" + order.OrderNo + "\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r" +
                    "病区：{2}\r" +
                    "床号：{3}\r" +
                    "日期：{4}\r" +
                    "餐点：{5}\r" +
                    "菜品数量：{6}\r" +
                    "总金额：{7} 元",
                    order.CardName,
                    order.CardNo,
                    order.WardName,
                    order.BedLabel,
                    order.DinnerDate,
                    "[" + order.FpName + "]" + order.HmtName,
                    order.FoodCount,
                    order.TotalMoney.GetValueOrDefault().ToString("F2"));

                message += "\r\n系统正在执行退费操作，稍后您的所缴纳的款项将会退回您的账户中";

                try
                {
                    //CustomApi.SendText(null, order.OpenId, message);
                    SendMessage.SendText(AppId, order.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送退餐成功消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送退餐失败消息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        /// <returns></returns>
        public static void SendUnDinnerFailedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.DinnerOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }

                var url = GetH5Url("/index/dinner-order-detail/" + order.Id);

                var message =
                    string.Format(
                    "退餐失败！\r" +
                    "订单号：" + order.OrderNo + "\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r" +
                    "病区：{2}\r" +
                    "床号：{3}\r" +
                    "日期：{4}\r" +
                    "餐点：{5}\r" +
                    "菜品数量：{6}\r" +
                    "总金额：{7} 元\r\n" +
                    "<a href='{8}'>点这里【查看点餐单】</a>",
                    order.CardName,
                    order.CardNo,
                    order.WardName,
                    order.BedLabel,
                    order.DinnerDate,
                    "[" + order.FpName + "]" + order.HmtName,
                    order.FoodCount,
                    order.TotalMoney.GetValueOrDefault().ToString("F2"),
                    url);

                try
                {
                    //CustomApi.SendText(null, order.OpenId, message);
                    SendMessage.SendText(AppId, order.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送退餐失败消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送退餐成功退费成功消息
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="retryCount"></param>
        public static void SendUnDinnerSucceedRefundSucceedMsg(int orderId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var order = ctx.DinnerOrders.FirstOrDefault(x => x.Id == orderId);
                if (order == null)
                {
                    return;
                }

                var message =
                    string.Format(
                    "退费成功！\r" +
                    "点餐单信息：\r" +
                    "订单号：" + order.OrderNo + "\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r" +
                    "病区：{2}\r" +
                    "床号：{3}\r" +
                    "日期：{4}\r" +
                    "餐点：{5}\r" +
                    "菜品数量：{6}\r" +
                    "总金额：{7} 元\r\n" +
                    "您所缴纳的款项已退回。支付宝支付即刻到账，银行卡支付7个工作日内到账。请注意查收",
                    order.CardName,
                    order.CardNo,
                    order.WardName,
                    order.BedLabel,
                    order.DinnerDate,
                    "[" + order.FpName + "]" + order.HmtName,
                    order.FoodCount,
                    order.TotalMoney.GetValueOrDefault().ToString("F2"));

                try
                {
                    //CustomApi.SendText(null, order.OpenId, message);
                    SendMessage.SendText(AppId, order.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送退餐成功退费成功消息出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送新缴费单消息
        /// </summary>
        /// <param name="cardId"></param>
        /// <param name="retryCount"></param>
        public static void SendNewBillMsg(int cardId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var card = ctx.HospCards.FirstOrDefault(x => x.CardId == cardId);
                if (card == null)
                {
                    return;
                }
                var user = ctx.WxUsers.FirstOrDefault(x => x.Id == card.WxUserId);
                if (user == null)
                {
                    return;
                }
                var billUrl = GetH5Url("/index/get-bill");
                var paperUrl = GetH5Url("/index/paper-list");

                var message = string.Format(
                    "新的缴费单\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r\n" +
                    "<a href='{2}'>点这里【缴费单列表】</a>\r\n" +
                    "您还可以对本次就诊过程做评价\r\n" +
                    "<a href='{3}'>点这里【满意度调查】</a>",
                    card.PatientName,
                    card.CardNo,
                    billUrl,
                    paperUrl);

                try
                {
                    //CustomApi.SendText(null, card.OpenId, message);
                    SendMessage.SendText(AppId, user.OpenId, message);

                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送新缴费单消息出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送新化验单消息
        /// </summary>
        /// <param name="cardId"></param>
        /// <param name="retryCount"></param>
        public static void SendNewInspectionMsg(int cardId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var card = ctx.HospCards.FirstOrDefault(x => x.CardId == cardId);
                if (card == null)
                {
                    return;
                }
                var user = ctx.WxUsers.FirstOrDefault(x => x.Id == card.WxUserId);
                if (string.IsNullOrEmpty(user?.OpenId) )
                {
                    return;
                }
                var url = GetH5Url("/index/get-inspection");

                var message = string.Format(
                    "新的化验单\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r\n" +
                    "<a href='{2}'>点这里【化验单列表】</a>",
                    card.PatientName,
                    card.CardNo,
                    url);

                try
                {
                    //CustomApi.SendText(null, card.OpenId, message);
                    SendMessage.SendText(AppId, user.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送新化验单消息出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送新检查单消息
        /// </summary>
        /// <param name="cardId"></param>
        /// <param name="retryCount"></param>
        public static void SendNewCheckMsg(int cardId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var card = ctx.HospCards.FirstOrDefault(x => x.CardId == cardId);
                if (card == null)
                {
                    return;
                }
                var user = ctx.WxUsers.FirstOrDefault(x => x.Id == card.WxUserId);
                if (user == null)
                {
                    return;
                }
                var url = GetH5Url("/index/get-check");

                var message = string.Format(
                    "新的检查单\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r\n" +
                    "<a href='{2}'>点这里【检查单列表】</a>",
                    card.PatientName,
                    card.CardNo,
                    url);

                try
                {
                    //CustomApi.SendText(null, card.OpenId, message);
                    SendMessage.SendText(AppId, user.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送新检查单消息出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        /// <summary>
        /// 发送新调查问卷消息
        /// </summary>
        /// <param name="cardId"></param>
        /// <param name="retryCount"></param>
        public static void SendNewPaperMsg(int cardId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var card = ctx.HospCards.FirstOrDefault(x => x.CardId == cardId);
                if (card == null)
                {
                    return;
                }
                var user = ctx.WxUsers.FirstOrDefault(x => x.Id == card.WxUserId);
                if (user == null)
                {
                    return;
                }
                var url = GetH5Url("/index/paper-list");

                var message = string.Format(
                    "满意度调查\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r\n" +
                    "你可以对今天的就诊过程做评价\r\n" +
                    "<a href='{2}'>点这里【满意度调查】</a>",
                    card.PatientName,
                    card.CardNo,
                    url);

                try
                {
                    //CustomApi.SendText(null, card.OpenId, message);
                    SendMessage.SendText(AppId, user.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送新调查问卷消息出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }

        public static void SendHaltMsg(string openId, string content)
        {
            SendMessage.SendText(AppId, openId, content);
        }

        public static void SendHaltMsg(string openId, string deptName, string doctorName, string cardName, string regDate, string sumFee)
        {
            //您好，张三医生在您预约的时段已停诊，请您重新挂号，我们将为您退费。
            //科室：呼吸内科
            //医生：张三
            //就诊人：李四
            //就诊时间：11月7日 10:00-11:00
            //挂号金额：10元
            //退款将在1-2个工作日内退回您的支付账户，请您稍后查询账户金额。
            //发送模板信息
            var bizContent = new
            {
                toUserId = openId,
                template = new
                {
                    templateId = TingZhen,
                    context = new
                    {
                        actionName = "",
                        headColor = HeadColor,
                        url = "",
                        keyword1 = new
                        {
                            color = keywordColor,
                            value = deptName
                        },
                        keyword2 = new
                        {
                            color = keywordColor,
                            value = doctorName
                        },
                        keyword3 = new
                        {
                            color = keywordColor,
                            value = cardName
                        },
                        keyword4 = new
                        {
                            color = keywordColor,
                            value = regDate
                        },
                        keyword5 = new
                        {
                            color = keywordColor,
                            value = sumFee
                        },
                        first = new
                        {
                            color = HeadColor,
                            value = $"您好，" + doctorName + "医生在您预约的时段已停诊，请您重新挂号，我们将为您退费。"
                        },
                        remark = new
                        {
                            color = HeadColor,
                            value = "退款将在1-2个工作日内退回您的支付账户，请您稍后查询账户金额。"
                        }
                    }

                }
            };

            try
            {
                //SendMessage.SendText(AppId, openId, message);
                SendMessage.SendTemplate(AppId, JsonConvert.SerializeObject(bizContent));
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("发送停诊信息时 Alipay SDK 出错，信息：{0}", JsonConvert.SerializeObject(ex));
            }
        }

        /// <summary>
        /// 发送入院消息
        /// </summary>
        /// <param name="cardId"></param>
        /// <param name="retryCount"></param>
        public static void SendInpatientMsg(int cardId, int retryCount = 0)
        {
            using (var ctx = new GdsfybjyWeiXinEntities())
            {
                var card = ctx.HospCards.FirstOrDefault(x => x.CardId == cardId);
                if (card == null)
                {
                    return;
                }
                var user = ctx.WxUsers.FirstOrDefault(x => x.Id == card.WxUserId);
                if (user == null)
                {
                    return;
                }
                var url = GetH5Url("/index/get-inpatient");

                var message = string.Format(
                    "入院缴费通知\r" +
                    "姓名：{0}\r" +
                    "卡号：{1}\r\n" +
                    "<a href='{2}'>点这里【查询预交金余额】</a>",
                    card.PatientName,
                    card.CardNo,
                    url);

                try
                {
                    //CustomApi.SendText(null, card.OpenId, message);
                    SendMessage.SendText(AppId, user.OpenId, message);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("发送入院消息出错，信息：{0}", JsonConvert.SerializeObject(ex));
                }
            }
        }
    }
}